﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;

namespace SignalR.Controllers
{

    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            List<string> list = new List<string>();
            list.Add("sss");
            //   list.Export();
            //list.ex
            return View();
        }
        public IActionResult Index2()
        {
            ExcelHelp excel = new ExcelHelp();
            excel.Compare();
            List<OnlineClient> list = new List<OnlineClient>();
            for (int i = 0; i < 100; i++)
            {
                if (i % 3 == 0)
                {
                    list.Add(new OnlineClient()
                    {
                        NickName = $@"朱由检{i}",
                        Avatar = $"QQ{i}",
                        Email = $"email{i}",
                        Mobile = "15220231801",
                    });
                    list.Add(new OnlineClient()
                    {
                        NickName = $@"朱由检{i}",
                        Avatar = $"QQ{i}{i}",
                        Email = $"email{i}",
                        Mobile = "15220231801",
                    });
                }
                else
                {
                    list.Add(new OnlineClient()
                    {
                        NickName = $@"朱由检{i}",
                        Avatar = $"QQ{i}",
                        Email = $"email{i}",
                        Mobile = "15220231801",
                    });
                }
            }
            var f = list.Export(headArray: new[] { "姓名", "QQ", "手机", "邮箱" }, c =>
            {

                ////首行标头 合并 整理
                c.CopyRow(0, 1);
                c.GetRow(0).Cells[1].SetCellValue("联系方式");
                c.AddMergedRegion(new CellRangeAddress(0, 0, 1, 3));



                //自动计算、合并行
                List<KeyValuePair<int, string>> nameIndexList = new List<KeyValuePair<int, string>>();


                //动态插入列
                var list = new List<KeyValuePair<string, string>>();
                list.Add(new KeyValuePair<string, string>("补考费", "100"));
                list.Add(new KeyValuePair<string, string>("报名费", "10"));
                list.Add(new KeyValuePair<string, string>("教材费", "50"));

                for (int i = c.FirstRowNum; i <= c.LastRowNum; i++)
                {
                    var v = c.GetRow(i).Cells[0].StringCellValue.Trim();
                    nameIndexList.Add(new KeyValuePair<int, string>(i, v));
                    if (i == 1)//第一行，把标题列插进去
                    {
                        for (int j = 0; j < list.Count(); j++)
                        {
                            var ce = c.GetRow(i).CreateCell(4 + j);
                            ce.SetCellValue(list[j].Key);
                            ce.CellStyle = c.GetRow(i).Cells[0].CellStyle;
                        }

                    }
                    if (i > 1)
                    {

                        for (int j = 0; j < list.Count(); j++)
                        {
                            var ce = c.GetRow(i).CreateCell(4 + j);
                            ce.SetCellValue(list[j].Value);
                        }
                    }


                }
                var names = nameIndexList.Select(c => c.Value).Distinct().ToList();
                foreach (var item in names)
                {
                    var startIndex = nameIndexList.Where(c => c.Value == item).Min(c => c.Key);
                    var endIndex = nameIndexList.Where(c => c.Value == item).Max(c => c.Key);
                    if (endIndex > startIndex)
                    {
                        c.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(startIndex, endIndex, 0, 0));
                    }
                }




            });
            return File(f.ToArray(), "application/vnd.ms-excel", "导出.xlsx");
        }

        [HttpGet]
        public IActionResult Export()//[FromBody] OrderDto dto
        {
            ExcelHelp excel = new ExcelHelp();
            DataTable dt = new DataTable("Test");
            dt.Columns.Add("姓名", typeof(string));
            dt.Columns.Add("性别", typeof(string));
            dt.Columns.Add("电话", typeof(string));


            dt.Rows.Add("李磊", "男", "15898653259");
            dt.Rows.Add("张执先", "男", "19158974563");
            dt.Rows.Add("韩志成", "女", "16498653245");

       
            var ms = excel.ExportDataTableToExcel(dt, "报表");
            ms.Flush();
            ms.Dispose();
            return File(ms.ToArray(), "application/vnd.ms-excel", "导出.xlsx");

        }


        //[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        //public IActionResult Error()
        //{

        //}
    }
}
